/**
 * @Author 12629
 * @Date 2022/4/1 21:17
 * @Description：
 */
public class TestBinarySearchTree {

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public  TreeNode root ;

    /**
     *
     * @param val
     * @return
     */
    public  boolean search(int val) {
        if(root == null) {
            return false;
        }
        TreeNode cur = root;
        while (cur != null) {
            if(cur.val == val) {
                return true;
            }else if(cur.val > val) {
                cur = cur.left;
            }else {
                cur = cur.right;
            }
        }
        return false;
    }

    /**
     *
     * @param val
     * @return
     */
    public  boolean insert(int val) {
        if(root == null) {
            root = new TreeNode(val);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else {
                return false;//不能插入相同的数字
            }
        }
        if(parent.val < val) {
            parent.right = new TreeNode(val);;
        }else {
            parent.left = new TreeNode(val);;
        }
        return true;
    }


    /**
     *
     * @param key
     */
    public boolean remove(int key) {
        if(root == null) {
            return false;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                removeNode(parent,cur);
                return true;
            }
        }
        return false;
    }

    public void removeNode(TreeNode parent,TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(parent.right == cur) {
                parent.right = cur.right;
            }else {
                parent.left = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(parent.left == cur) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(target == targetParent.left) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }

    public void inorder(TreeNode root) {
        if(root == null) return;
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.right);
    }

    public void preOrder(TreeNode root) {
        if(root == null) return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }


    public static void main(String[] args) {
        TestBinarySearchTree testBinarySearchTree
                = new TestBinarySearchTree();
        testBinarySearchTree.insert(13);
        testBinarySearchTree.insert(4);
        testBinarySearchTree.insert(17);
        testBinarySearchTree.insert(5);

        testBinarySearchTree.inorder(testBinarySearchTree.root);
        System.out.println();
        testBinarySearchTree.preOrder(testBinarySearchTree.root);
        System.out.println("======================");

        testBinarySearchTree.remove(13);

        System.out.println("======================");
        testBinarySearchTree.inorder(testBinarySearchTree.root);
        System.out.println();
        testBinarySearchTree.preOrder(testBinarySearchTree.root);
    }

}
